﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Orid.Data.SqlServer;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class OperatorRole
    {
        #region 虚函数
        public virtual List<Model.ModelOperatorRole> GetOperatorRole()
        {
            return null;
        }
        #endregion

        protected List<Model.ModelOperatorRole> GetOperatorRole(string connectString, string accountTableName, string accountInRoleTableName)
        {
            List<Model.ModelOperatorRole> modelOperatorRoleList = new List<Model.ModelOperatorRole>();
            try
            {
                //[FutureManagement_Masters].[FutureMa].[AccountInRole]
                //[FutureManagement_Masters].[FutureMa].[Accounts]
                StringBuilder sqlText = new StringBuilder();
                sqlText.Append("   SELECT [AccountInRole].[RoleID] ,[Accounts].[LoginName] ")  ;
                sqlText.Append("    FROM " + accountInRoleTableName  + "  [AccountInRole] with(NOLOCK) ");
                sqlText.Append("    INNER JOIN " + accountTableName +"  [Accounts] with(NOLOCK) ");
                sqlText.Append("     ON [AccountInRole].AccountID = [Accounts].AccountID ");

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();

                    SqlDataReader reader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString());
                    while (reader.Read())
                    {
                        Model.ModelOperatorRole operatorRole = new Model.ModelOperatorRole();

                        if (reader["RoleID"] != DBNull.Value)
                        {
                            operatorRole.RoleID = Convert.ToInt32(reader["RoleID"]);
                        }

                        if (reader["LoginName"] != DBNull.Value)
                        {
                            operatorRole.OperatorID = Convert.ToString(reader["LoginName"]);
                        }


                        modelOperatorRoleList.Add(operatorRole);
                    }

                    reader.Close();

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();


                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }

            return modelOperatorRoleList;
           
        }
    }
}
